<?php

namespace Home\Controller;

class StoreHouseController extends HomeController {
    
    /**
     * 权限校验
     * @var unknown
     */
    /*
    protected $config = array(
        'app_type'	=> 'common', 
        'admin'		=> 'read,delete,inc,addPost,addHalfPost,editPost,validate,dec,halfdec,category,product,product_type,addProduct,addProductType,addProductTypePost,addProductPost,deleteType,editType,delProduct,editProduct,saveProduct,saveProductTypePost,showNum,show_childtask,show_user,check,indelete,inreject,inreceive',
    );
    */
    protected $flag = array('1'=>'成品','2'=>'半成品');
	
    /**
     * 仓库首页(non-PHPdoc)
     * @see \Home\Controller\HomeController::index()
     */
	function index() {
		
	    $map = array();
	    if (!empty($_REQUEST['type'])) {
	        $map['type'] = array('eq',  $_REQUEST['type']);
	    }
	    if (!empty($_REQUEST['product_id'])) {
	        $map['product_id'] = array('eq',  $_REQUEST['product_id']);
	    }
	    if (!empty($_REQUEST['product_type_id'])) {
	        $map['product_type_id'] = array('eq',  $_REQUEST['product_type_id']);
	    }
	    if (!empty($_REQUEST['product_name'])) {
	    	$map['product_name'] = array('like', '%'. trim($_REQUEST['product_name']) . '%');
	    }
	    //$map['status'] = 1;// 审核成功的
	    $type = empty($_REQUEST['type']) ? '' : $_REQUEST['type'];
	    $product_id = empty($_REQUEST['product_id']) ? '' : $_REQUEST['product_id'];
	    $product_type_id = empty($_REQUEST['product_type_id']) ? '' : $_REQUEST['product_type_id'];
	    $product_name = empty($_REQUEST['product_name']) ? '' : $_REQUEST['product_name'];
	    $product = M('storehouse_product')->field('id,product')->select();
	    $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name')->select();
	    $model = M('storehouse');
	    $this -> assign('auth', $this -> config['auth']);
	    $this -> _list($model, $map);
	    $this -> assign('product',$product);
	    $this -> assign('type',$type);
	    $this -> assign('product_id',$product_id);
	    $this -> assign('product_type_id',$product_type_id);
	    $this -> assign('product_type',$productType);
	    $this -> assign('product_name',$product_name);
	    $this-> assign('flag',$this->flag);
	    $this->display();
	}
	
	 /**
	  * 详情(non-PHPdoc)
	 * @see \Home\Controller\HomeController::read()
	 */
	function read($id) {
        
	    if (empty($id)) {
	    	R('StoreHouse/index');
	    }
	    $storeHouse = M('storehouse');
	    $map['id'] = $id;
	    $data = $storeHouse->where($map)->find();
	    $this->assign('list',$data);
	    $this -> assign('auth', $this->config['auth']);
	    $this -> assign('cr', ($data['type']==1 ? '入库':'出库'));
	    $this->display();
	}
	
	/**
	 * 删除
	 * @param unknown $id
	 */
	public function delete($id) {

	    $storeHouse = M('storehouse');
	    $map['id'] = $id;
	    $status = $storeHouse->where($map)->delete();
	    if ($status) {
	        $this -> success("成功删除1条!");
	    } else {
	        $this -> error('删除失败!');
	    }
	}
	
	/**
	 * 增加入库
	 */
	function inc() {
	    
	    $storeProduct = M('storehouse_product');
	    $product = $storeProduct->getField("id,product,product_bh");
	    $this->assign('opreate_name',get_user_name());
	    $this->assign('product',$product);
	    $this->display();
	}
	
	 /**
	  * 添加
	 */
	function addPost() {
		
	    $d = $_POST;
	    $this->validate($d);
	    $storeProduct = M('storehouse_product');
	    $productType = $storeProduct->field('id,product,product_bh,product_type_id,flag,product_type_name,convert_unit,convert_number')->where('id='.$d['storehouse_product_id'])->find();
	    if(empty($productType)) {
	       $this->output(false,'请输产品');
	    }
	    if(!empty($_FILES['product_img']['name'])) {
	       $upload = new \Think\Upload();// 实例化上传类
	       $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	       $upload->rootPath  =     './Uploads/store/'; // 设置附件上传根目录
	       $info = $upload->upload();// 上传文件
	       if (!$info) { // 上传错误提示错误信息 
	           $this->output(false,'上传图片失败');
	       }
	    }
	    $storeHouse = M('storehouse');
	    $data = array(
	        'product_id'=>$productType['id'],
            'product_name' => $productType['product'],
            'product_bh' => $productType['product_bh'],
	        'product_type_id' => $productType['product_type_id'],
	        'product_type_name' => $productType['product_type_name'],
	        'flag' => $productType['flag'],
	        'number'  => $d['number'],
	        'type' => $d['type'],
	        'user_id' => get_user_id(),
	        'user_name' => get_user_name(),
	        'opreate_name' => $d['opreate_name'],
	        'remark' => $d['remark'],
	        'update_at' => date('Y-m-d H:i:s',time()),
	        'create_at' => date('Y-m-d H:i:s',time())
	    );
	    
	    if ($d['type']==2) {
	        if (!empty($productType['convert_unit']) && !empty($productType['convert_number'])) {
	            $data['convert_unit'] = $productType['convert_unit'];
	            $data['operate_number'] = $d['number']/$productType['convert_number'];
	        }        	
	    }
	    
	    if (!empty($info['product_img']['savepath']) && !empty($info['product_img']['savename'])) {
	        $data['product_img'] = $info['product_img']['savepath'].$info['product_img']['savename'];
	    }
	    $storeHouse->startTrans();
	    $status = $storeHouse->add($data);
	    if ($status) {
	        $storehouse_product = M('storehouse_product');
	        if ($d['type']==1) {        // 入库
	            $flag = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('num',$d['number']);
	            $gg = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('in_num',$d['number']);
	        } else if ($d['type']==2) { //  出库
	            $flag = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setDec('num',$d['number']);
	            $gg = $storehouse_product -> where('id='.$d['storehouse_product_id']) -> setInc('out_num',$d['number']);
	        }
	    	if ($status && $flag) {
	    	    $storeHouse->commit();
	    	    $this -> success("添加成功",U('index'));
	    	} else {
	    	    $storeHouse->rollback();
	    	    $this -> error("添加失败",U('index'));
	    	}
	    } else {
	        $storeHouse->rollback();
	        $this -> error("添加失败",U('index'));
	    }
	}
	
	/**
	 * 半成品出库给任务人的处理
	 */
	public function addHalfPost() {

		$d = $_POST;
		$this->validate($d);
		if (empty($d['task_id'])) {
			$this->output(false,'请选择任务');
		}
		if (empty($d['task_user_id'])) {
			$this->output(false,'请选择接收人');
		}
		
		$storeProduct = M('storehouse_product');
		$productType = $storeProduct->field('id,product,product_bh,product_type_id,product_type_name,convert_unit,convert_number')->where('id='.$d['storehouse_product_id'])->find();
		if(empty($productType)) {
			$this->output(false,'请输产品');
		}
		if(!empty($_FILES['product_img']['name'])) {
			$upload = new \Think\Upload();// 实例化上传类
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =     './Uploads/store/'; // 设置附件上传根目录
			$info = $upload->upload();// 上传文件
			if (!$info) { // 上传错误提示错误信息
				$this->output(false,'上传图片失败');
			}
		}
		$storeHouse = M('storehouse');
		$user_list = array();
		foreach ($d['task_id'] as $key=>$val) {
			if(empty($val) || empty($d['task_user_id'][$key])){
				$this->output(false,'任务数据没选择');
			}
			$executor = explode("|", $d['task_user_id'][$key]);
			if (count($executor) !=2) {
				$this->output(false,'接收人格式有误');
			}
			$user_list[] = $executor[1];
			$data[$key] = array(
				'product_id'=>$productType['id'],
				'product_name' => $productType['product'],
				'product_bh' => $productType['product_bh'],
				'product_type_id' => $productType['product_type_id'],
				'product_type_name' => $productType['product_type_name'],
				'number'  => $d['number'],
				'type' => $d['type'],
				'user_id' => get_user_id(),
				'user_name' => get_user_name(),
				'flag' => 2,  //半成品出库给任务人
				'status' => 2, // 待审核
				'task_id' => $d['task_id'][$key],
				'to_user_id' => $executor[1] ,
				'to_user_name' => $executor[0],
				'opreate_name' => $d['opreate_name'],
				'remark' => empty($d['remark']) ? "半成品出库" : $d['remark'],
				'update_at' => date('Y-m-d H:i:s',time()),
				'create_at' => date('Y-m-d H:i:s',time())
			);
			// 转化单位
			if (!empty($productType['convert_unit']) && !empty($productType['convert_number'])) {
				$data[$key]['convert_unit'] = $productType['convert_unit'];
				$data[$key]['operate_number'] = $d['number']/$productType['convert_number'];
			}
		}
		
		$status = $storeHouse->addAll($data);
		if ($status) {
			//发送消息
			$push_data['type'] = '仓库';
			$push_data['action'] = '需要执行';
			$push_data['title'] = "来自：" . get_dept_name()."-".get_user_name();
			$push_data['content'] = "标题：仓库发货" ;
			$push_data['url'] = U('Task/read',"id={$d['task_id']}&return_url=Task/index");
			send_push($push_data, $user_list);
			$this -> success("成功",U('index'));
		} else {
			$this -> error("失败",U('index'));
		}
	}
	
	/**
	 * 编辑
	 */
	public function editPost() {
	    
	    $d = $_POST;
	     if (empty($d['id'])) {
	        $this->output(false,'请选择编辑产品');
	    }
	    $this->validate($d);
	    $storeProduct = M('storehouse_product');
	    $productType = $storeProduct->field('id,product,product_bh')->where('id='.$d['storehouse_product_id'])->find();
	    if(empty($productType)) {
	        $this->output(false,'请输产品');
	    }
	    $storeHouse = M('storehouse');
	    $map['id'] = $d['id'];
	    $data = array(
            'product' => $productType['product'],
	        'product_bh' => $productType['product_bh'],
            'number'  => $d['number'],
	        'opreate_name' => $d['opreate_name'],
	        'remark' => $d['remark'],
	        'update_at' => date('Y-m-d H:i:s',time()),
	    );
	    $status = $storeHouse->where($map)->save($data);
	    if ($status) {
	        $this->output(true);
	    } else {
	        $this->output(false,'更新失败，请刷新重试');
	    }
	}
	
	/**
	 * 验证
	 * @param unknown $d
	 */
	function validate($d) {
	    
	    if (empty($d['storehouse_product_id'])) {
	        $this->output(false,'请输产品');
	    }
	    if (empty($d['number'])) {
	        $this->output(false,'请输产品数量');
	    }
	    if (!is_numeric($d['number'])) {
	        $this->output(false,'产品数量必须为数字');
	    }
	    if (empty($d['opreate_name'])) {
	        $this->output(false,'请输入操作人');
	    }
// 	    if (empty($d['remark'])) {
// 	        $this->output(false,'请输入摘要');
// 	    }
	}
	
	 /**
	  * 增加出库
	 */
    function dec() {
        
        $map['flag'] = 1; // 1成品2半成品
        $storeProduct = M('storehouse_product');
        $product = $storeProduct->where($map)->getField("id,product,product_bh,product_img");
        $this->assign('opreate_name',get_user_name());
        $this->assign('product',$product);
	    $this->display();
    }
    
     /**
      * 半成品出库
     */
    function halfdec() {

        $map['flag'] = 2; // 1成品2半成品
        $storeProduct = M('storehouse_product');
        $product = $storeProduct->getField("id,product,product_bh,product_img");
        $taskParent = M('task_parent')->field('id,task_parent_name')->where('is_del=0')->select();
        $this->assign('opreate_name',get_user_name());
        $this->assign('product',$product);
        $this->assign('taskParent',$taskParent);
        $this->display();
    }
    
    /**
     *类别统计
    */
    function category() {
        	
        $model = M("storehouse_product");
        $map = $this -> _search($model);
        $select = 'product_type_id, product_type_name ,SUM(num) AS num, SUM(in_num) AS in_num, SUM(out_num) AS out_num';
        $this -> _list($model, $map,$sort='',$group = 'product_type_id',$select);
        $cate = array();
        $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name,flag')->select();
        foreach ($productType as $val) {
            $cate[$val['id']] = $val['flag'];
        }
        $this-> assign('product_type',$productType);
        $this-> assign('product_type_id',$_REQUEST['eq_product_type_id']);
        $this-> assign('product_type_name',$_REQUEST['li_product_type_name']);
        $this-> assign('cate',$cate);
        $this-> assign('flag',$this->flag);
        $this->display();
    }
    
    
    
    
    /**
      * 入库的产品
    */
    function product() {
        
        $this -> assign('auth', $this -> config['auth']);
        $model = M("storehouse_product");
        $map = $this -> _search($model);
        $this -> _list($model, $map);
        $cate = array();
        $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name,flag')->select();
        foreach ($productType as $val) {
            $cate[$val['id']] = $val['flag'];
        }
        $this->assign('product_type',$productType);
        $this-> assign('product',$_REQUEST['li_product']);
        $this-> assign('product_bh',$_REQUEST['li_product_bh']);
        $this-> assign('product_type_id',$_REQUEST['eq_product_type_id']);
        $this-> assign('flag',$this->flag);
        $this-> assign('cate',$cate);
        $this->display();
    }
    
    /**
     * 库存的产品类型
     */
    function product_type() {

    	$model = M("storehouse_product_type");
    	$map = $this -> _search($model);
    	$this -> _list($model, $map);
    	$this -> assign('auth', $this -> config['auth']);
    	$this->display();
    }
    
    /**
     * 添加入库产品
    */
    function addProduct() {
        
        $productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name,flag')->select();
        $this-> assign('flag',$this->flag);
        $this->assign('product_type',$productType);
        $this->display();
    }
    
    /**
     * 添加库存类别
     */
    function addProductType() {

        $this->assign('flag',$this->flag);
        $this->display();
    }
    
    /**
     * 添加库存的类别
    */
    function addProductTypePost() {
            
        $d = $_POST;
        if (empty($d['type_name'])) {
            $this->output(false,'请输入库存类别名称');
        }
        if (empty($d['flag']) || !in_array( $d['flag'], array(1,2))) {
            $this->output(false,'请输入分类');
        }
        
        $map['type_name'] = trim($d['type_name']);
        $storeProductType = M('storehouse_product_type');
        $result = $storeProductType->where($map)->select();
        if (!empty($result)) {
            $this -> error("库存类别已经存在",U('addProductType'));
        }
        $data = array(
            'type_name' => trim($d['type_name']),
            'user_id' => get_user_id(),
            'flag'    => $d['flag'],
            'create_at' =>  date('Y-m-d H:i:s',time())
        );
       
        $status = $storeProductType->add($data);
        if ($status) {
            $this -> success("添加成功",U('product_type'));
        } else {
            $this -> error("添加失败",U('addProductType'));
        }
    }
    
     /**
      * 添加产品
     */
    function addProductPost(){

        $d = $_POST;
        if (empty($d['product_type_id'])) {
            $this -> error("添加失败,请输入库存类别",U('addProduct'));
        }
        if (empty($d['product'])) {
            $this -> error("添加失败,请输入产品",U('addProduct'));
        }
        if (empty($d['product_bh'])) {
            $this -> error("添加失败,请输入产品编号",U('addProduct'));
        }
        if (!empty($d['convert_number'])) {
            
            if (!is_numeric ($d['convert_number'])) {
                $this -> error("转换数量必须为整数",U('addProduct'));
            }
            if (empty($d['convert_unit'])) {
                $this -> error("转换单位不能为空",U('addProduct'));
            }
        }
        $typeWhere = array(
            'id' => $d['product_type_id']	
        );
        $where = array(
            'product' => $d['product'],
            'product_bh' => $d['product_bh'],
        );
        $storeProduct = M('storehouse_product');
        $result = $storeProduct->field('id')->where($where)->select();
        if (!empty($result)) {
            $this -> error("添加失败,该产品型号已经添加",U('addProduct'));
        }
        $storeProductType = M('storehouse_product_type')->where($typeWhere)->field('id,type_name,flag')->find();
        if (empty($storeProductType)) {
            $this -> error("添加失败,库存类别不存在",U('addProduct'));
        }
        $data = array(
            'product' => $d['product'],
            'product_bh' => $d['product_bh'],
            'user_id' => get_user_id(),
            'user_name' => get_user_name(),
            'product_type_id' => $storeProductType['id'],
            'product_type_name' =>  $storeProductType['type_name'],
            'type_name' => $storeProductType['type_name'],
            'flag'      => $storeProductType['flag'],
            'create_at' =>  date('Y-m-d H:i:s',time())
        ) ;
        
        if(!empty($_FILES['product_img']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/store/'; // 设置附件上传根目录
            $info   =   $upload->upload();// 上传文件
            if(!$info) {// 上传错误提示错误信息
                $this -> error("上传图片失败",U('product'));
            }
        }
        if (!empty($info['product_img']['savepath']) && !empty($info['product_img']['savename'])) {
            $data['product_img'] = $info['product_img']['savepath'].$info['product_img']['savename'];
        }
        if (!empty($d['convert_number']) && !empty($d['convert_unit'])) {
            $data['convert_number'] = $d['convert_number'];
            $data['convert_unit'] = $d['convert_unit'];
        }        
        $status = $storeProduct->add($data);
        if ($status) {
            $this -> success("添加成功",U('index'));
        } else {
            $this -> error("添加失败",U('addProduct'));
        }
        
    }
    
    /**
     * 删除库存的产品类别
     */
    function deleteType() {
                
        $id = $_POST['id'];
        if(empty($id)) {
            $this->output(false,'删除失败');
        }
        $product_type = M('storehouse_product_type');
        $map['id'] = $id;
        $status = $product_type->where($map)->delete();
        if ($status) {
            $this->output(true,'删除成功');
        } else {
            $this->output(false,'删除失败');
        }         
    }
    
    /**
     * 编辑库存的产品的类别
    */
    function editType($id) {
            
        if (empty($id)) {
            R('StoreHouse/product_type');
        }
        $product_type = M('storehouse_product_type');
        $map['id'] = $id;
        $data = $product_type->where($map)->find();
        $this->assign('result',$data);
        $this->assign('flag',$this->flag);
        $this -> assign('auth', $this->config['auth']);
        $this->display();
    }
    
     /**
     * 删除入库产品
     * @param unknown $id
     */
    function delProduct() {
    	
    	$id = $_POST['id'];
    	if(empty($id)) {
    		$this->output(false,'删除失败');
    	}
    	$product = M('storehouse_product');
        $map['id'] = $id;
        $status = $product->where($map)->delete();
        if ($status) {
        	$this->output(true,'删除成功');
        } else {
        	$this->output(false,'删除失败');
        }
    } 
	
     /**
     * 编辑入库产品
     * @param unknown $id
     */
    function editProduct($id) {
		
    	if (empty($id)) {
    		$this->error('请选择编辑的产品');
    	}
    	$product = M('storehouse_product');
    	$map['id'] = $id;
    	$result = $product->where($map)->field('id,product,product_bh,product_img,product_type_id')->find();
    	if (empty($result)) {
    		$this->error('请选择编辑真实有效产品');
    	}
    	$productType = M('storehouse_product_type')->where('is_del=0')->field('id,type_name,flag')->select();
    	$this->assign('result',$result);
    	$this->assign('product_type',$productType);
    	$this-> assign('flag',$this->flag);
    	$this->display();
    }
    
    /**
     * 保存入库产品
     */
    function saveProduct(){

    	$d = $_POST;
    	if (empty($d['id'])) {
    		$this->output(false,'请选择编辑的产品');
    	}
    	if (empty($d['product_type_id'])) {
    	    $this -> error("添加失败,请输入库存类别",U('addProduct'));
    	}
    	if (empty($d['product'])) {
        	$this->output(false,'请输入产品');
        }
        if (empty($d['product_bh'])) {
            $this->output(false,'请输入产品编号');
        }
        $typeWhere = array(
            'id' => $d['product_type_id']
        );
        $storeProductType = M('storehouse_product_type')->where($typeWhere)->field('id,type_name,flag')->find();
        if (empty($storeProductType)) {
            $this -> output(false,"添加失败,库存类别不存在");
        }
        $product = M('storehouse_product');
        $map['id'] = $d['id'];
        $data = array(
            'product_type_id' => $storeProductType['id'],
            'product_type_name' =>  $storeProductType['type_name'],
            'flag' => $storeProductType['flag'],
        	'product' => $d['product'],
        	'product_bh' => $d['product_bh']
            
        );
        $status = $product->where($map)->save($data);
        if ($status) {
        	$this->output(true);
        } else {
        	$this->output(false,'保存失败，请刷新再试');
        }
    }
    
    /**
     * 保存入库产品的类别
    */
    function saveProductTypePost() {

        $d = $_POST;
        
        if (empty($d['id'])) {
            $this->output(false,'请选择编辑的库存类型');
        }
        if (empty($d['type_name'])) {
            $this->output(false,'请输入库存类别');
        }
        
        if (empty($d['flag']) || !in_array( $d['flag'], array(1,2))) {
            $this->output(false,'请输入分类');
        }
        
        $product_type = M('storehouse_product_type');
        $map['id'] = $d['id'];
        $data = array(
            'type_name' => $d['type_name'],
            'flag'      => $d['flag']
        );
        $status = $product_type->where($map)->save($data);
        if ($status) {
            $this->output(true,'保存成功');
        } else {
            $this->output(false,'保存失败，请刷新再试');
        } 
    }
    
    /**
     * 显示库存
     */
    function showNum() {

        if (empty($_POST['id'])) {
            $this->output(false,'请选择产品');
        }
        $storeProduct = M('storehouse_product');
        $product = $storeProduct->field('id,product,num,in_num,out_num,convert_unit,convert_number')->where('id='.$_POST['id'])->find();
        if (empty($product)) {
            $this->output(false,'请选择真实有效产品');
        } else {
            $this->output(true,$product);
        }  
    }
    
    /**
     * 显示子流程
     */
    function show_childtask() {
        
        if (empty($_POST['id'])) {
            $this->output(false,'请选择任务');
        }
        $map['pid'] = $_POST['id'];
        $map['is_del'] = 0;
        $childtask = M('task')->where($map)->field('id,name')->select();
        $this->output(true,$childtask);
    }
    
    function show_user(){
    	
        if (empty($_POST['id'])) {
            $this->output(false,'请选择任务');
        }
        $map['id'] = $_POST['id'];
        $map['is_del'] = 0;
        $userRes = M('task')->where($map)->field('id,executor')->find();
        $user_list = array();
        if (strpos($userRes['executor'], "dept_") !== false) { 
            $tmp = explode('|', $userRes['executor']);
            $executor = $tmp[1];
            $executor = str_replace('dept_', '',$executor);
            $where['dept_id']=array('eq',$executor);
            $dept_user_list = M('User')->where($where)->field('id,name')->select();
            foreach($dept_user_list as $val){
               $user_list[] = $val['name'].'|'.$val['id'];
            }    	
        } else {
            $user_list = explode(";", rtrim($userRes['executor'],';'));
        }
        $this->output(true,$user_list);
    }
    
    // 审核入库
    function check() {

    	$map = array();
    	$into_check_user_name = get_user_name().'|'.get_user_id().';';
    	$into_dept = get_dept_name().'|'.'dept_'.get_dept_id().';';
    	
    	if (!empty($_REQUEST['product'])) {
    		$map['product'] = array('like',  '%' . trim($_REQUEST['product']) . '%');
    	}
    	$map['is_del'] = array('eq',0);  // 0 代表未删除
    	$map['type']   = array('eq',0);  // 0代表入库
    	$map['into_check_user_name'] = array(['like',  '%' .$into_check_user_name.'%'],['like',  '%' .$into_dept.'%'],'or');
    	
    	$product = empty($_REQUEST['product']) ? '' : $_REQUEST['product'] ;
    	
    	$model = M('transfer_or_instore_log');
    	$this -> _list($model, $map);

    	
    	$this -> assign('product',$product);
    	$this->display();
    }
    
    // 删除
    function indelete() {
    	
    	$d = $_POST;
    	if (empty($d['log_id'])) {
    		$this->output(false,'请选择删除记录');
    	}
    	$map['id'] = $d['log_id'];
    	$map['type'] = 0;  //0入库|1转交
    	$data['is_del'] = 1;
    	$status = M('transfer_or_instore_log')->where($map)->save($data);
    	if ($status) {
    		$this->output(true,'成功');
    	} else {
    		$this->output(false,'失败');
    	}
    }
    
    // 拒绝
    function inreject() {
    	
    	$d = $_POST;
    	if (empty($d['log_id'])) {
    		$this->output(false,'请选择拒绝记录');
    	}
    	$map['id'] = $d['log_id'];
    	$map['type'] = 0;  //0入库|1转交
    	$data['status'] = 1;
    	$status = M('transfer_or_instore_log')->where($map)->save($data);
    	if ($status) {
    		$this->output(true,'成功');
    	} else {
    		$this->output(false,'失败');
    	}
    }
    
    // 接受
    function inreceive() {
    	
    	$d = $_POST;
    	if (empty($d['log_id'])) {
    		$this->output(false,'请选择接收记录');
    	}
    	if (empty($d['num'])) {
    		$this->output(false,'入库产品数量不能为空');
    	}
    	$num = (int)$d['num'];
    	$transfer_or_instore_log = M('transfer_or_instore_log');
    	$storehouse_product = M('storehouse_product');
    	$storehouse= M('storehouse');
    	$task_product = M('task_product');
    	$task_parent = M('task_parent');
    	$task_children = M('task');
    	
    	$instoreRes = $transfer_or_instore_log->field('id,task_product_id,task_id,number')->where('id='.$d['log_id'])->find();
    	if (empty($instoreRes)) {
    		$this->output(false,'请选择接收记录');
    	}
    	
    	$taskChildrenRes = $task_children->field('id,pid')->where('id='.$instoreRes['task_id'])->find();
    	if (empty($taskChildrenRes)) {
    		$this->output(false,'该生产任务不存在');
    	}
    	
    	$taskProRes = $task_product->field('id,task_id,is_check_in,storehouse_product_id') -> where('id='.$instoreRes['task_product_id'])->find();
    	if (empty($taskProRes)) {
    		$this->output(false,'入库产品与任务产品为空');
    	}
    	
    	$storehouseProductRes = $storehouse_product->field('id,product,product_bh,product_img,product_type_id,product_type_name,convert_unit,convert_number')->where('id='.$taskProRes['storehouse_product_id'])->find();
		if (empty($storehouseProductRes)) {
			$this->output(false,'入库产品不存在');
		} 
		$transfer_or_instore_log->startTrans();
		
		$map['id'] = $d['log_id'];
		$map['type'] = 0;  //0入库|1转交
		$data['status'] = 2;
		$data['number'] = $num;
		$transferStatus = M('transfer_or_instore_log')->where($map)->save($data);
		
		$flag = $storehouse_product -> where('id='.$taskProRes['storehouse_product_id']) -> setInc('num',$num);
		$gg = $storehouse_product -> where('id='.$taskProRes['storehouse_product_id']) -> setInc('in_num',$num);
		$ee = $task_parent -> where('id='.$taskChildrenRes['pid']) -> setInc('finish_product',$num);
		
		$data = array(
			'product_id'=>$storehouseProductRes['id'],
			'product_name' => $storehouseProductRes['product'],
			'product_bh' => $storehouseProductRes['product_bh'],
			'product_type_id' => $storehouseProductRes['product_type_id'],
			'product_type_name' => $storehouseProductRes['product_type_name'],
			'number'  => $num,
			'type' => 1 ,  // 1入库 | 2出库
			'user_id' => get_user_id(),
			'user_name' => get_user_name(),
			'opreate_name' => get_user_name(),
			'remark' => '任务流程同意入库操作',
			'update_at' => date('Y-m-d H:i:s',time()),
			'create_at' => date('Y-m-d H:i:s',time())
		);
		$storeId =  $storehouse->add($data);
		
		
    	if ($transferStatus && $flag && $gg && $storeId && $ee) {
    		$transfer_or_instore_log->commit();
    		$this->output(true,'成功');
    	} else {
    		$transfer_or_instore_log->rollback();
    		$this->output(false,'失败');
    	}
    }
    

}
